package demospringbootstarter.config;

import demospringbootstarter.properties.DemoProperties;
import demospringbootstarter.service.DemoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

//@Configuration声明为一个配置类，没什么好说的
@Configuration
//该注解用来开启上面一步 @ConfigurationProperties 注解配置bean的支持，
//就是告诉springboot能够支持@ConfigurationProperties，
// 或者你也可以在@ConfigurationProperties注解标识的类上添加@Configuration和@Component，效果一样
@EnableConfigurationProperties(DemoProperties.class)
//@ConditionalOnProperty用来控制@Configuration是否生效，简单来说也就是我们可以通过在yml或properties配置文件中控制 @Configuration 注解的配置类是否生效。
@ConditionalOnProperty(prefix = "demo", name = "isopen", havingValue = "true")
public class DemoConfig {

    @Autowired
    private DemoProperties properties;

    @Bean
    public DemoService demoService() {
        return new DemoService(properties.getSayWhat(), properties.getToWho());
    }
}
